home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / progwin.zip / ABOUT2.ZIP / ABOUT2.C < prev    next >
C/C++ Source or Header  |  1991-10-22  |  5KB  |  187 lines

  1.  /*------------------------------------------
  2.      ABOUT2.C -- About Box Demo Program No. 2
  3.          (c) Charles Petzold, 1990
  4.    -------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include "about2.h"
  8.  
  9. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG);
  10.  
  11. short nCurrentColor = IDD_BLACK,
  12.     nCurrentFigure = IDD_RECT;
  13.  
  14. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  15.                  LPSTR lpszCmdLine, int nCmdShow)
  16.      {
  17.      static char szAppName [] = "About2";
  18.      MSG        msg;
  19.      HWND        hwnd;
  20.      WNDCLASS   wndclass;
  21.  
  22.      if (!hPrevInstance)
  23.         {
  24.         wndclass.style        = CS_HREDRAW | CS_VREDRAW;
  25.         wndclass.lpfnWndProc    = WndProc;
  26.         wndclass.cbClsExtra    = 0;
  27.         wndclass.cbWndExtra    = 0;
  28.         wndclass.hInstance    = hInstance;
  29.         wndclass.hIcon        = LoadIcon (hInstance, szAppName);
  30.         wndclass.hCursor        = LoadCursor (NULL, IDC_ARROW);
  31.         wndclass.hbrBackground    = GetStockObject (WHITE_BRUSH);
  32.         wndclass.lpszMenuName    = szAppName;
  33.         wndclass.lpszClassName    = szAppName;
  34.  
  35.         RegisterClass (&wndclass);
  36.         }
  37.  
  38.      hwnd = CreateWindow (szAppName, "About Box Demo Program",
  39.                     WS_OVERLAPPEDWINDOW,
  40.                     CW_USEDEFAULT, CW_USEDEFAULT,
  41.                     CW_USEDEFAULT, CW_USEDEFAULT,
  42.                     NULL, NULL, hInstance, NULL);
  43.  
  44.      ShowWindow (hwnd, nCmdShow);
  45.      UpdateWindow (hwnd);
  46.  
  47.      while (GetMessage (&msg, NULL, 0, 0))
  48.         {
  49.         TranslateMessage (&msg);
  50.         DispatchMessage (&msg);
  51.         }
  52.      return msg.wParam;
  53.      }
  54.  
  55.    void PaintWindow (HWND hwnd, short nColor, short nFigure)
  56.      {
  57.      static DWORD dwColor [8] = { RGB (0,    0, 0), RGB (0,     0, 255),
  58.                         RGB (0,   255, 0), RGB (0,   255, 255),
  59.                         RGB (255,   0, 0), RGB (255,   0, 255),
  60.                         RGB (255, 255, 0), RGB (255, 255, 255) };
  61.  
  62.      HBRUSH    hBrush;
  63.      HDC        hdc;
  64.      RECT        rect;
  65.  
  66.      hdc = GetDC (hwnd);
  67.      GetClientRect (hwnd, &rect);
  68.      hBrush = CreateSolidBrush (dwColor [nColor - IDD_BLACK]);
  69.      hBrush = SelectObject (hdc, hBrush);
  70.  
  71.      if (nFigure == IDD_RECT)
  72.         Rectangle (hdc, rect.left, rect.top, rect.right, rect.bottom);
  73.      else
  74.         Ellipse   (hdc, rect.left, rect.top, rect.right, rect.bottom);
  75.  
  76.      DeleteObject (SelectObject (hdc, hBrush));
  77.      ReleaseDC (hwnd, hdc);
  78.      }
  79.  
  80.    void PaintTheBlock (HWND hCtrl, short nColor, short nFigure)
  81.      {
  82.      InvalidateRect (hCtrl, NULL, TRUE);
  83.      UpdateWindow (hCtrl);
  84.      PaintWindow (hCtrl, nColor, nFigure);
  85.      }
  86.  
  87. BOOL FAR PASCAL AboutDlgProc (HWND hDlg, WORD message, WORD wParam, LONG lParam)
  88.      {
  89.      static HWND    hCtrlBlock;
  90.      static short  nColor, nFigure;
  91.  
  92.      switch (message)
  93.         {
  94.         case WM_INITDIALOG:
  95.             nColor = nCurrentColor;
  96.             nFigure = nCurrentFigure;
  97.  
  98.              CheckRadioButton (hDlg, IDD_BLACK, IDD_WHITE, nColor);
  99.              CheckRadioButton (hDlg, IDD_RECT,  IDD_ELL,   nFigure);
  100.  
  101.              hCtrlBlock = GetDlgItem (hDlg, IDD_PAINT);
  102.              SetFocus (GetDlgItem (hDlg, nColor));
  103.              return  FALSE;
  104.  
  105.         case WM_COMMAND:
  106.              switch (wParam)
  107.                 {
  108.                 case IDOK:
  109.                    nCurrentColor = nColor;
  110.                    nCurrentFigure = nFigure;
  111.                    EndDialog (hDlg, TRUE);
  112.                    return TRUE;
  113.  
  114.                 case IDCANCEL:
  115.                    EndDialog (hDlg, FALSE);
  116.                    return TRUE;
  117.  
  118.                 case IDD_BLACK:
  119.                 case IDD_RED:
  120.                 case IDD_GREEN:
  121.                 case IDD_YELLOW:
  122.                 case IDD_BLUE:
  123.                 case IDD_MAGENTA:
  124.                 case IDD_CYAN:
  125.                 case IDD_WHITE:
  126.                    nColor = wParam;
  127.                    CheckRadioButton (hDlg, IDD_BLACK, IDD_WHITE, wParam);
  128.                    PaintTheBlock (hCtrlBlock, nColor, nFigure);
  129.                    return TRUE;
  130.  
  131.                 case IDD_RECT:
  132.                 case IDD_ELL:
  133.                    nFigure = wParam;
  134.                    CheckRadioButton (hDlg, IDD_RECT, IDD_ELL, wParam);
  135.                    PaintTheBlock (hCtrlBlock, nColor, nFigure);
  136.                    return TRUE;
  137.                 }
  138.              break;
  139.         case WM_PAINT:
  140.              PaintTheBlock (hCtrlBlock, nColor, nFigure);
  141.              break;
  142.         }
  143.      return FALSE;
  144.      }
  145.  
  146. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  147.      {
  148.      static FARPROC   lpfnAboutDlgProc;
  149.      static HANDLE    hInstance;
  150.      PAINTSTRUCT    ps;
  151.  
  152.      switch (message)
  153.         {
  154.         case WM_CREATE:
  155.              hInstance = ((LPCREATESTRUCT) lParam) ->hInstance;
  156.  
  157.              lpfnAboutDlgProc = MakeProcInstance (AboutDlgProc, hInstance);
  158.              return 0;
  159.  
  160.         case WM_COMMAND:
  161.              switch (wParam)
  162.                 {
  163.                 case IDM_ABOUT:
  164.                    if (DialogBox (hInstance, "AboutBox", hwnd,
  165.                             lpfnAboutDlgProc));
  166.                       InvalidateRect (hwnd, NULL, TRUE);
  167.                    return 0;
  168.                 }
  169.              break;
  170.  
  171.         case WM_PAINT:
  172.              BeginPaint (hwnd, &ps);
  173.              EndPaint (hwnd, &ps);
  174.              PaintWindow (hwnd, nCurrentColor, nCurrentFigure);
  175.              return 0;
  176.  
  177.         case WM_DESTROY:
  178.              PostQuitMessage (0);
  179.              return 0;
  180.         }
  181.      return DefWindowProc (hwnd, message, wParam, lParam);
  182.      }
  183.  
  184.  
  185.  
  186.  
  187.